import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:interview/config/app.enum.dart';
import 'package:interview/controller/navigation_controller.dart';
import 'package:interview/servers/login.dart';
import 'package:interview/theme/app_colors.dart';
import 'package:interview/views/home/components/clock_in.dart';


class MineUserinfo extends StatefulWidget {
  final AppColors theme;
  final User? user;
  const MineUserinfo({super.key, required this.theme, required this.user});

  @override
  State<MineUserinfo> createState() => _MineUserinfoState();
}

class _MineUserinfoState extends State<MineUserinfo> {
  final navigation = Get.find<NavigationController>();

  @override
  Widget build(BuildContext context) {
    // 判断用户是否存在
    final hasUser = widget.user != null;
    final avatar = hasUser ? widget.user!.avatar : '${AppEnumAssets.basePath}ic_mine_avatar.svg';
    final username = hasUser ? widget.user!.username : "登录";
    return SizedBox(
      width: double.infinity,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        mainAxisSize: MainAxisSize.min,
        children: [
          GestureDetector(
            onTap: (){
              if(!hasUser){
                navigation.push('/login');
              }
            },
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                hasUser?  
                CircleAvatar( radius: 28,  backgroundImage: NetworkImage(avatar)):
                SvgPicture.asset(avatar ,width: 56, height: 56, ),
                const SizedBox(width: 12),
                Text(username,style: TextStyle(fontSize: 14 , fontWeight: FontWeight.bold, color: widget.theme.contrastMix3),)
              ],
            ),
          ),
          ClockIn()
        ],
      ),
    );
  }
}